<?php
class CartAction extends Action {
    private $cart = null;
    private $nav = null;
    private $order = null;
    private $address = null;
    private $goods = null;
    private $delivery = null;
    public function __construct() {
        parent::__construct();
        $this->cart = new Cart();
        $this->nav = new NavModel();
        $this->order = new OrderModel();
        $this->address = new AddressModel();
        $this->goods = new GoodsModel();
        $this->delivery = new DeliveryModel();
    }

    public function index() {
        $this->tpl->assign('TenNav',$this->nav->findFrontTenNav());
        $this->tpl->assign('myCart',$this->cart->showProduct());
        $this->tpl->assign('total',$this->cart->getTotalCost());
        $this->tpl->display(SMARTY_FRONT.'public/cart.html');
    }

    public function flow() {
        $this->tpl->assign('allDelivery',$this->delivery->findFlowDelivery());
        $this->tpl->assign('TenNav',$this->nav->findFrontTenNav());
        $this->tpl->assign('myCart',$this->cart->showProduct());
        $this->tpl->assign('total',$this->cart->getTotalCost());
        $this->tpl->assign('address',$this->address->findOne());
        $this->tpl->display(SMARTY_FRONT.'public/flow.html');
    }

    public function order() {
        if(isset($_POST['send'])) {
            if($this->order->isCart()) {
                $id = $this->order->nextId();      //获取即将新增进数据库的id
                if($this->order->order()) {
                    $this->goods->setInventory();
                    if($_POST['pay'] == '支付宝') {
                        $this->redirect->succ('?a=member&m=alipay&id='.$id);
                    } else if($_POST['pay'] == '银行转账/汇款') {
                        $this->redirect->succ('?a=member&m=bank&id='.$id);
                    } else if($_POST['pay'] == '货到付款') {
                        $this->redirect->succ('?a=member&m=intime&id='.$id);
                    }
                }
            }
        }
    }

    public function addProduct() {
        if(!isset($_COOKIE['user'])) $this->redirect->succ('?a=member&m=login');      //需要登录才能购买
        if($this->cart->addProduct()) echo 1;
    }

    public function delPro() {
        if($this->cart->delPro()) $this->redirect->succ('?a=cart');
    }

    public function clearCar() {
        if($this->cart->clearCar()) $this->redirect->succ('?a=cart');
    }

    public function changeNum() {
        $this->cart->changeNum();
    }

    public function isNoMore() {
        $this->goods->isNoMore();
    }

}



?>